/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.udt.generator.entity.data;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.api.RefObject;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspElementDataType;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.DataTypeDataGenerator;
import com.inspur.edp.cef.generator.overall.CefGeneratorContext;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
import com.inspur.edp.udt.entity.ISimpleUdtData;
import com.inspur.edp.udt.entity.IUdtData;
import com.inspur.edp.udt.generator.entity.UdtNodeGeneratorContext;

import java.lang.reflect.Type;
import java.util.ArrayList;

public class UdtDataGenerator extends DataTypeDataGenerator {
    private UdtNodeGeneratorContext nodeGenContext;

    public UdtDataGenerator(UdtNodeGeneratorContext nodeGenContext) {
        super(nodeGenContext, nodeGenContext.getDataType());
        this.nodeGenContext = nodeGenContext;
        nodeGenContext.getEntityGenContext().setRootNodeType(new MediateType(getNameSpace(), getName()));
        nodeGenContext.getEntityGenContext().setRootNodeCode(nodeGenContext.getDataType().getCode());
    }

    @Override
    protected ArrayList<TypeInfo> addParentInterfaces() {
        java.util.ArrayList<TypeInfo> list = super.addParentInterfaces();
        if (list == null) {
            list = new java.util.ArrayList<TypeInfo>();
        }
        if (!((UnifiedDataTypeDef) nodeGenContext.getDataType() instanceof SimpleDataTypeDef)) {
            list.add(new TypeInfo(IUdtData.class));
        }
        return list;

    }

    private String getNameSpace() {
        return nodeGenContext.getEntityGenContext().getDefaultPackageName();
    }

    @Override
    protected ArrayList<TypeInfo> addExtendParentInterfaces() {
        ArrayList<TypeInfo> list = super.addExtendParentInterfaces();
        if (list == null)
            list = new ArrayList<TypeInfo>();
        TypeInfo parent = getGeneratedParentType((UnifiedDataTypeDef) nodeGenContext.getDataType(), nodeGenContext.getEntityGenContext().getUdtGeneratorContext());
        if (parent != null)
            list.add(parent);
        return list;
    }

    private TypeInfo getGeneratedParentType(UnifiedDataTypeDef udt, CefGeneratorContext cefGenContext) {
        if (!(udt instanceof SimpleDataTypeDef)) {
            return null;
        }

        TypeInfo genType = null;

        IGspCommonField field = ((SimpleDataTypeDef) udt).getContainElements().get(0);
        if (field.getObjectType() == GspElementObjectType.None) {
            if(field.getMDataType()== GspElementDataType.Boolean)
                genType=new TypeInfo(Boolean.class);
            else
            genType = Utils.getNativeType(field, cefGenContext);
        } else if (field.getObjectType() == GspElementObjectType.Enum) {
            String typeName = getContext().getDataTypeGeneratorContext().getEnumTypes().get(field.getLabelID());
            genType = new TypeInfo(typeName);
        } else if (field.getObjectType() == GspElementObjectType.Association) {
            String typeName = getContext().getDataTypeGeneratorContext().getAssociationTypes().get(field.getLabelID());
            genType = new TypeInfo(typeName);
        }

        TypeInfo type = new TypeInfo(ISimpleUdtData.class);
        type.setIsParameterizedType(true);
        type.getArguments().add(genType);
        return type;
    }
}
